home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / icon / packages.lha / packages / atari / ats.arc / TESTS.ARC / ENDETAB1.ICN < prev    next >
Text File  |  1990-03-28  |  4KB  |  102 lines

  1. ## Test driver for entab and detab
  2. #
  3. #  Input is read from standard input.  Commentary and error reports go to
  4. #  standard output.
  5. #
  6. #  Input lines are first preprocessed by interpreting escape sequences \a, \b,
  7. #  \n, \r, and \t and trimming a trailing '$' character.
  8. #  
  9. #  Input lines beginning with "=" establish tab stop settings.  Each numeric
  10. #  field specifies a tab stop, according to the entab/detab specs.
  11. #  
  12. #  All other lines are passed through entab and then detab, and the results are
  13. #  checked.  The characters "!" and "." are replaced by spaces before calling
  14. #  entab; "!" positions are expected to be replaced by tabs, with "." positions
  15. #  disappearing.  For example, "abcd!...ijk" tests that entab("abcd    ijk")
  16. #  returns "abcd\tijk".
  17. #  
  18. #  The result of each entab call is then passed to detab, with results expected
  19. #  to match the original entab argument (or its detab, if it had any tabs).
  20.  
  21. procedure main ()
  22.    params := setup ("=")        # start with default tabs (no args)
  23.    while line := escape (read ()) do {    # read and preprocess line
  24.       if line[1] == "=" then
  25.          params := setup (line)        # '=' line sets tab stops (arg list)
  26.       else {
  27.          s := map (line, "!.", "  ")    # turn "!." characters into spaces
  28.          params[1] := s
  29.          t := invoke (entab, params)    # run entab
  30.          if t ~== interp (line) then {    # check results
  31.             write ("entab failed for: ", map(line,"\t\r\n\b\007","!RNBA"))
  32.             write ("  returned value: ", map(t,   "\t\r\n\b\007","!RNBA"))
  33.          } else {
  34.             if upto ('\t', s) then    # detab input if it had a tab
  35.                s := invoke (detab, params)
  36.             params[1] := t
  37.             t := invoke (detab, params)    # detab the result of the entab
  38.             if t ~== s then {        # compare results
  39.                write ("detab failed for: ", map(line,"\t\r\n\b\007","!RNBA"))
  40.                write ("  returned value: ", map(t,   "\t\r\n\b\007","!RNBA"))
  41.                }
  42.             }
  43.          }
  44.    }
  45.    end
  46.  
  47. procedure escape (line)        # interpret escape sequences and trim one '$'
  48.    if line[-1] == "$" then
  49.       line := line[1:-1]
  50.    s := ""
  51.    line ? 
  52.       while not pos (0) do {
  53.          s ||:= tab (upto ('\\') | 0)
  54.          s ||:= (="\\" & case (c := move(1)) of {
  55.         "a": "\007"
  56.             "b": "\b"
  57.             "n": "\n"
  58.             "r": "\r"
  59.             "t": "\t"
  60.             default: "\\" || c
  61.          })
  62.       }
  63.    return s
  64.    end
  65.  
  66. procedure interp (pattern)    # interpret metacharacters '!.'
  67.    s := ""
  68.    pattern ? 
  69.       while not pos (0) do {
  70.          tab (many ('.'))
  71.          s ||:= tab (upto ('.') | 0)
  72.       }
  73.    return map (s, "!", "\t")
  74.    end
  75.  
  76. procedure setup (line)        # interpret and report a column spec line
  77.    p := [&null]
  78.    line ? while tab (upto (&digits)) do
  79.       put (p, integer (tab (many (&digits))))
  80.    writes ("testing entab/detab(s")
  81.    every writes (",", \!p)
  82.    write (")")
  83.    return p
  84.    end
  85.  
  86. procedure invoke (func, a)    # invoke a function with a list of up to 10 args
  87.    return case *a of {
  88.       0:  func ()
  89.       1:  func (a[1])
  90.       2:  func (a[1], a[2])
  91.       3:  func (a[1], a[2], a[3])
  92.       4:  func (a[1], a[2], a[3], a[4])
  93.       5:  func (a[1], a[2], a[3], a[4], a[5])
  94.       6:  func (a[1], a[2], a[3], a[4], a[5], a[6])
  95.       7:  func (a[1], a[2], a[3], a[4], a[5], a[6], a[7])
  96.       8:  func (a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8])
  97.       9:  func (a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9])
  98.       10: func (a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10])
  99.       default: stop ("too many args for invoke")
  100.    }
  101.    end
  102.